//
//  DYMeishiCell.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/2/3.
//  Copyright © 2021 叁拾叁. All rights reserved.
//


import UIKit

class DYMeishiCell: SYBaseCell , UICollectionViewDelegate, UICollectionViewDataSource{
    
    var collectionView: UICollectionView!
    
    var models:[DYRestaurantsModel] = []

    public func configureView(_ models: [DYRestaurantsModel]) {
        self.models = models
    }
    
    override func initViews() {
        
        setCollectionView()
        contentView.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(autoWidth(-15.0))
            make.left.right.top.equalToSuperview()
            make.height.equalTo(125.0.autoWidth())
        }
        
        
    }
    
    func setCollectionView() {
        
            
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        let itemWidth = 145.0
        layout.itemSize = CGSize(width: itemWidth, height: 125)
//        layout.minimumLineSpacing = 10
//        layout.minimumInteritemSpacing = 10
        layout.scrollDirection = .horizontal
        collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .white
        collectionView.showsHorizontalScrollIndicator = false
        
        
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(DYMeishiSubCell.self, forCellWithReuseIdentifier: NSStringFromClass(DYMeishiSubCell.self))
        self.addSubview(self.collectionView)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return models.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: DYMeishiSubCell? = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(DYMeishiSubCell.self), for: indexPath) as? DYMeishiSubCell
        cell?.configureView(models[indexPath.row])
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = DYMeishiDetailViewController(self.models[indexPath.row].id ?? "")
        self.currentVC.navigationController?.pushViewController(vc, animated: true)
    }
    
  
}





// MARK: - DYMeishiSubCell

class DYMeishiSubCell: SYBaseCollectionViewCell {
    
    let wenLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x1E1E1E)
        v.font = 15.autoFontSize()
        return v
    }()
    
    let pic: UIImageView = {
        let v: UIImageView = UIImageView()
        v.layer.cornerRadius = 6
        v.layer.masksToBounds = true
        return v
    }()
    

    public func configureView(_ model: DYRestaurantsModel) {
        wenLb.text = model.name
        let urls = model.imgUrl?.components(separatedBy: ",")
        pic.sy_setWithUrl(urls?.first)
    }
    
    override func initViews() {
        
        addSubview(wenLb)
        addSubview(pic)
        wenLb.snp.makeConstraints { (make) in
            make.top.equalTo(pic.snp.bottom).offset(9.5.autoWidth())
            make.left.equalTo(pic.snp.left)
            make.right.equalTo(pic)
            make.height.equalTo(14.5.autoWidth())
            make.bottom.equalToSuperview()
        }
        pic.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview().offset(15.0.autoWidth())
            make.right.equalToSuperview()
            make.height.equalTo(100.0.autoWidth())
            make.width.equalTo(130.0.autoWidth())
        }
    }
}

